package com.bp.hearthealth.pulse.lnapps.bp_ui.bp_fragment.bp_lifestyle.bp_adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bp.hearthealth.pulse.lnapps.databinding.LifestyleIndicatorLayoutBgBinding
import com.bp.hearthealth.pulse.lnapps.bp_ui.bp_fragment.bp_lifestyle.bp_bean.BpIndicatorDataBean
import com.bp.hearthealth.pulse.lnapps.bp_utils.DoubleClickListener

class BpLifeStyleIndicatorAdapter : RecyclerView.Adapter<BpLifeStyleIndicatorAdapter.ItemVH>() {
    private lateinit var onCliclLisener: OnItemClickListener
    private var dataList: List<BpIndicatorDataBean> = arrayListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemVH {
        return ItemVH(
            LifestyleIndicatorLayoutBgBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    fun setListData(list: MutableList<BpIndicatorDataBean>) {
        this.dataList = list
        notifyDataSetChanged()
    }

    inner class ItemVH(val binding: LifestyleIndicatorLayoutBgBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindData(item: BpIndicatorDataBean) {
            binding.bean = item
            binding.executePendingBindings()
        }
    }

    override fun onBindViewHolder(holder: ItemVH, @SuppressLint("RecyclerView") position: Int) {
        if (position == 0) {
            dataList[position].selected = true
        }
        holder.bindData(dataList[position])
        holder.itemView.setOnClickListener(object : DoubleClickListener() {
            override fun onNoDoubleClick(v: View?) {
                for (i in dataList.indices) {
                    dataList[i].selected = i == position
                }
                onCliclLisener.onItemClick(position)
            }
        })
    }

    fun updateIndicator(position: Int) {
        for (i in dataList.indices) {
            dataList[i].selected = i == position
        }
    }

    fun setOnItemClickLisener(clickListener: OnItemClickListener) {
        this.onCliclLisener = clickListener
    }

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}

